﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */
using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Configuration.Install;
using Microsoft.TeamFoundation.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;

//Intallation : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.Exe CheminEtNomAssembly
//add-pssnapin ServerTFSPSSnapIn
//


namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Récupère un serveur TFS
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "TFS")]
    public class GetTFS : Cmdlet
    {
        TeamFoundationServer tfs = null ;
        
        public GetTFS()
        {
            this.tfs = TFSProvider.s_TFSDriveInfo != null ? TFSProvider.s_TFSDriveInfo.TFS : null;
        }

        protected override void ProcessRecord()
        {
            try
            {
                if (this.tfs != null)
                    WriteObject(tfs);
                else
                {
                    tfs = new TeamFoundationServer(url, new UICredentialsProvider());

                    try
                    {
                        tfs.Authenticate();
                    }
                    catch
                    {
                        tfs = new TeamFoundationServer(url, new UICredentialsProvider());
                    }

                    WriteObject(tfs);
                }
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(null, ex.StackTrace);
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Get-TFS is end");
            base.EndProcessing();
        }

        [Parameter(Position = 0)]
        [ValidateNotNullOrEmpty]
        public String url { get; set; }


        [Parameter(Position = 1)]
        [ValidateNotNullOrEmpty]
        public bool Login { get; set; }

    }

    /// <summary>
    /// Permet l'installation des CmdLet
    /// </summary>
    [RunInstaller(true)]
    public class ServerTFSPSSnapIn : PSSnapIn
    {
        public ServerTFSPSSnapIn()
            : base()
        {
        }

        /// <summary>
        /// Nom des cmdlet
        /// </summary>
        public override String Name
        {
            get
            {
                return "ServerTFSPSSnapIn";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override String Vendor
        {
            get
            {
                return "choulant.blogspot.com  : Noham CHOULANT";
            }

        }
        public override String VendorResource
        {
            get
            {
                return "ServerTFSPSSnapIn, choulant.blogspot.com";
            }

        }

        /// <summary>
        /// Description des cmdlet
        /// </summary>
        public override String Description
        {
            get
            {
                return "Create TFS object";
            }

        }
        public override string DescriptionResource
        {
            get
            {
                return "Create TFS object";
            }
        }

    }
}
